import { defineStore } from "pinia";
import Axios from "../Axios/index";
// 用于加载各种数据
export const main = defineStore("main", {
  state: () => {
    return {
      // 计时器用于游戏主页面计算经验
      ExpTimer: null,
      // 判断当前经验池是否到达上线 
      EXPFlag: true,
      // 控制经验倒计时内从
      timerExpTime: 60,
      MP3: false,  // 控制音乐播放
      MozillaText: false, // 判断当前登录设备 false平板端  true  移动端
      Screen: 2, // 控制浏览器是否全屏显示， 1表示开启全屏 2表示关起全屏
      // 角色信息数据
      Character: {
        uID: "", // 角色id
        nickname: "", // 角色昵称
        aTK: "", // 角色攻击值
        dEF: "", // 角色防御值
        hP: "", // 角色血量
        eXP: "", // 角色当前经验
        realm: "", // 角色境界信息
        realmChildren: "", // 角色境界标识

      },
      // 境界信息
      Realm: [
        // {
        //   realmId: 10, // 境界id
        //   realmName: "元婴前期", // 境界名称
        //   aTK: 8, // 境界增加攻击力
        //   dEF: 8, // 境界增加防御力
        //   hP: 15, // 境界增加血量
        //   eXP: 40, // 升级下一级所需经验
        //   timeExp: 5.3, // 每分钟增加经验数
        // },
      ],
      // 物品信息
      item: [
        // {
        // "itemId":"dan21",  标识
        // "itemName":"化形丹", 名称
        // "itemDetails": "金丹修士服用可增加220点经验", 描述
        // "itemNum": 1, 数量
        // "itemType":"丹药", 分类
        // "realmBigId":6,  绑定的大境界id
        // "itemEvaluate":3 // 评级
        // },
      ],
      // 职位信息
      profession: [
        // {
        //   "professionId": "",  // 职业标识
        //   "professionName": "", // 职业名称
        //   "professionStatus": "", // 职业状态
        //   "professionDetails": "", // 职业描述
        //   "professionBranch": [
        //     {
        //       "occupationId": 1, // 职业小境界标识
        //       "occupationProfessionId": 1,  // 绑定境界标识
        //       "occupationName": "", // 小境界名称
        //       "occupationRealm": 3, // 绑定修仙境界id
        //       "occupationEXP": "",  // 升级下一个境界所需经验
        //       "occupationDetails": "" // 境界描述
        //     }

        //   ]
        // }
      ],
      // 商店信息
      shop: {
        id: "", // 绑定的用户信息
        time: "", // 上一次刷新时间
        item: [{}], // 物品
      },
      croplands: [{
        itemChildren: {}
      }], // 农田信息
      // 七日签到信息
      SignIn: {},
      // 获取npc信息
      Player: {},
      // 获取地图信息
      MapList: {},
      // 获取剧情信息
      Plot: {}
    };
  },
  actions: {

    // 获取所有剧情信息
    async ScenarioAll() {
      const res = await Axios("/Game/ScenarioAll", "POST");
      if (res.code == 200) {
        this.Plot = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 获取地图信息
    async AtlasAll() {
      const res = await Axios("/Game/AtlasAll", "POST");
      if (res.code == 200) {
        this.MapList = res.data;
        return true;
      } else {
        return false;
      }
    },

    // 获取全局npc 信息
    async PlayerUid() {
      const res = await Axios("/Game/PlayerUid", "POST");
      if (res.code == 200) {
        this.Player = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 获取七日签到信息
    async SignInformation() {
      const res = await Axios("/Game/SignInformation", "POST");
      if (res.code == 200) {
        this.SignIn = res.data;

        return true;
      } else {
        return false;
      }
    },
    // 获取农田信息
    async CroplandId() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/CroplandId", "POST");
      if (res.code == 200) {
        this.croplands = res.data;
        return true;
      } else {
        return false;
      }
    },

    // 刷新商店
    async ShopRefresh() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/ShopRefresh", "POST");
      if (res.code == 200) {
        this.shop = res.data;
        if (this.shop.item.length > 0 && this.shop) {
          let i = 0
          this.shop.item.forEach(el => {
            el.index = i
            i++
          });
        }
        return true;
      } else {
        return false;
      }
    },

    // 判断当前登录设备，不对移动端和平板端做判别
    Mozilla() {
      const userAgent = navigator.userAgent;
      this.MozillaText = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
    },
    async Breakthrough() {
      const res = await Axios("/Game/Breakthrough", "POST");
      return res
    },
    // 获取所有职业信息
    async ProfessionSelectAll() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/ProfessionSelectAll", "POST");
      if (res.code == 200) {
        this.profession = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 获取所有物品信息
    async ItemSelectAll() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/ItemSelectAll", "POST");
      if (res.code == 200) {
        this.item = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 生成/获取角色信息
    async ADDCHARACTERS() {
      const res = await Axios("/Game/CharacterAdd", "POST");
      if (res.code == 200) {
        this.Character = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 删除角色信息
    async DelectCHARACTERS() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/CharacterDelete", "POST");
      if (res.code == 200) {
        return true;
      } else {
        return false;
      }

    },
    // 加载游戏境界
    async REALM() {
      // const res = await Axios("/Game/CharacterInformation", "POST");
      const res = await Axios("/Game/RealmSelectAll", "POST");
      if (res.code == 200) {
        this.Realm = res.data;
        return true;
      } else {
        return false;
      }
    },
    // 创建角色
    async CREATECHARACTERS() {
      const one = await this.DelectCHARACTERS();
      const two = await this.ADDCHARACTERS();
      const three = await this.REALM();
      const four = await this.ItemSelectAll();
      const five = await this.ProfessionSelectAll();
      const six = await this.ShopRefresh();
      const seven = await this.CroplandId();
      const eight = await this.SignInformation();
      const nine = await this.PlayerUid();
      const ten = await this.ScenarioAll();

      if (one && two && three && four && five && six && seven && eight && nine && ten) {
        return true;
      } else {
        return false;
      }
    },
    // 新增角色经验

    async AddExP(id) {
      const res = await Axios("/Game/CharacterExp", "POST", id);
      if (res.code == 200 && res.data) {
        return true;
      } else {
        return false;
      }

    },
    // 继续游戏
    async ContinueGame() {
      const two = await this.ADDCHARACTERS();
      const three = await this.REALM();
      const four = await this.ItemSelectAll();
      const five = await this.ProfessionSelectAll();
      const six = await this.ShopRefresh();
      const seven = await this.CroplandId();
      const eight = await this.SignInformation();
      const nine = await this.PlayerUid();
      const ten = await this.ScenarioAll();
      if (two && three && four && five && six && seven && eight && nine && ten) {
        return true;
      } else {
        return false;
      }
    },
    // 使用额外经验
    async timerExpFn() {
      const res1 = await this.AddExP(1);
      this.EXPFlag = res1
      if (res1) {
        this.ExpTimer ? clearInterval(this.ExpTimer) : ""
        this.ExpTimer = setInterval(() => {
          this.timerExpTime--
          if (this.timerExpTime <= 0) {
            // 调用修仙等级
            this.timerExpTime = 60

            setTimeout(async () => {
              const res = await this.AddExP(2);
              this.EXPFlag = res
              if (res) {
                this.ADDCHARACTERS()
              } else {
                this.ExpTimer ? clearInterval(this.ExpTimer) : ""
                return
              }

            }, 1000)
          }
        }, 1000)
      } else {
        this.ExpTimer ? clearInterval(this.ExpTimer) : ""
        this.timerExpTime = 60
      }

    }
  },
});
